package C042;

/**
 * ClassName: C01
 * Package: C042
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/2 21:18
 * @Version 1.0
 * @Since 1.0
 */
public class C01 {
    public static void main(String[] args) {
        int n = 1000;
        int random = 0;
        int max = 10;
        for (int i = 0; i < n; i++) {
            random =  2 * (int)(Math.random() * max) - max;
            int num1 = process(random);
            int num2 = f(random);
            int num3 = process2(random);
            if(num1 != num2 || num1 != num3) {
                System.out.println("error !!!!" + random + " " + num1 + " " + num2 + " " + num3);
                return;
            }
        }
        System.out.println("测试成功");
        for (int i = 0; i < 100; i++) {
            System.out.println(i + ":" + (process(i) == Integer.MAX_VALUE ? -1 : (process(i))));
        }
    }

    public static int process2(int apple) {
        if ((apple & 1) != 0) {
            return Integer.MAX_VALUE;
        }
        if (apple < 18) {
            if (apple == 0) {
                return 0;
            }
            if (apple == 6 || apple == 8) {
                return 1;
            }
            if (apple == 12 || apple == 14 || apple == 16) {
                return 2;
            }
            return Integer.MAX_VALUE;
        }
        return (apple - 18) / 8 + 3;
    }


    public static int process (int rest) {
        if(rest < 0) {
            return Integer.MAX_VALUE;
        }
        if(rest == 0) {
            return 0;
        }

        int num1 = process(rest - 8);
        int num2 = process(rest - 6);
        num1 += num1 == Integer.MAX_VALUE ? 0 : 1;
        num2 += num2 == Integer.MAX_VALUE ? 0 : 1;
        return Math.min(num1, num2);
    }


    // 当前还有rest个苹果，使用的每个袋子必须装满，返回至少几个袋子
    public static int f(int rest) {
        if (rest < 0) {
            return Integer.MAX_VALUE;
        }
        if (rest == 0) {
            return 0;
        }
        // 使用8规格的袋子，剩余的苹果还需要几个袋子，有可能返回无效解
        int p1 = f(rest - 8);
        // 使用6规格的袋子，剩余的苹果还需要几个袋子，有可能返回无效解
        int p2 = f(rest - 6);
        p1 += p1 != Integer.MAX_VALUE ? 1 : 0;
        p2 += p2 != Integer.MAX_VALUE ? 1 : 0;
        return Math.min(p1, p2);
    }
}
